/*
Copyright 2012 Martin Sliwka

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

#include "stdafx.h"

#include "AsyncNamedPipe.h"

int _tmain(int argc, _TCHAR* argv[])
{
	if (argc != 2)
	{
		char ModuleName[MAX_PATH*2] = {};
		::GetModuleFileNameA(NULL, ModuleName, MAX_PATH*2);

		char Drive[MAX_PATH] = {};
		char Dir[MAX_PATH] = {};
		char Name[MAX_PATH] = {};
		char Ext[MAX_PATH] = {};
		_splitpath_s(ModuleName, Drive, Dir, Name, Ext);

		printf("%s - Named pipes helper for Windows\n", Name);
		printf("\n");
		printf("Usage: %s pipename\n", Name);
		printf("\n");
		printf("pipename\t- Full pipe name (ie. \\\\.\\pipe\\pipe.name)\n");
		printf("\n");
		
		return 0;
	}

	::SetConsoleCP(::GetACP());
	::SetConsoleOutputCP(::GetACP());

	AsyncNamedPipePtr PipeA = std::make_shared<AsyncNamedPipeT>();
	AsyncNamedPipePtr PipeB = std::make_shared<AsyncNamedPipeT>();

	HRESULT Result = S_OK;

	if (SUCCEEDED(Result))
		Result = PipeA->Create(argv[1], true);

	if (SUCCEEDED(Result))
		Result = PipeB->Create(argv[1], false);

	if (SUCCEEDED(Result))
		Result = PipeA->StartTransfer(PipeB);
	
	if (SUCCEEDED(Result))
		Result = PipeB->StartTransfer(PipeA);

	PipeA->WaitForCompletion(INFINITE);
	PipeB->WaitForCompletion(INFINITE);

	PipeA->Destroy();
	PipeB->Destroy();
}

